import Pyro.core
import sys
import pserv
import math
from pserv import Mergeable, copy_remote_file

# Pyro client usage:
# python pclient.py uri get key
# python pclient.py uri put key value

ubiqis = Pyro.core.getProxyForURI(sys.argv[1])

if sys.argv[2] == 'get':
    (value,source) = ubiqis.query(sys.argv[3])
    if isinstance(value,Mergeable):
        print value.value
    else:
        print value
elif sys.argv[2] == 'sharefile':
    ubiqis.share_file(sys.argv[3])
elif sys.argv[2] == 'copyfile':
    copy_remote_file(ubiqis,sys.argv[3],sys.argv[4])
elif sys.argv[2] == 'put':
    ubiqis.store(sys.argv[3],sys.argv[4])
elif sys.argv[2] == 'putl':
    m = Mergeable(set())
    m.merge(sys.argv[4])
    ubiqis.store(sys.argv[3],m)
elif sys.argv[2] == 'read':
    net = ubiqis.read_network({},sys.argv[3])
    for host in net:
        print host+":"+str(net[host]['key'])
        for k in net[host]['known']:
            v = pserv.shahex('foo')
            p = math.log(abs(pserv.shahex(k)-v))
            print "  ",k,'[',net[host]['known'][k],']',p
        for k in net[host]['data']:
            print "  ",k,"=>",net[host]['data'][k]
